package com.example.socket.test;

import com.example.socket.Servlet;
import com.example.socket.ServletRequest;
import com.example.socket.ServletResponse;
import com.example.socket.model.MulitiFile;
import com.example.util.FileUtils;

import java.io.OutputStream;
import java.util.Map;

/**
 * @Classname FindServlet
 * @Description TODO
 * @Date 2022/8/18 10:06
 * @Created by XiongXiong
 * @Author: X.I.O
 */
public class FindServlet extends Servlet {

    @Override
    public void doGet(ServletRequest request, ServletResponse response) {

    }

    @Override
    public void doPost(ServletRequest request, ServletResponse response) {
        try {
            Map<String,Object> formData=request.getFormData();
            if(formData != null){
                for (Map.Entry<String, Object> entry : formData.entrySet()) {
                    System.out.println(entry.getKey()+" form  "+entry.getValue());
                    if(entry.getValue() instanceof MulitiFile){
                        MulitiFile file = (MulitiFile) entry.getValue();
                        System.out.println(file.getFileName()+" "+file.getFileType());
                        FileUtils.wirteBytesByPath(file.getBytes(),"D:\\有道云笔记\\"+file.getFileName());
                    }
                }
            }
            System.out.println(request.getPath());
            System.out.println(request.getUrl());


            String responseText = "{\"message\": \"Hello, world!\"}";
            byte[] responseBytes = responseText.getBytes("UTF-8");

            OutputStream outputStream = response.getOutputStream();
            outputStream.write("HTTP/1.1 200 OK\r\n".getBytes("UTF-8"));
            outputStream.write("Content-Type: application/json\r\n".getBytes("UTF-8"));
            outputStream.write(("Content-Length: " + responseBytes.length + "\r\n").getBytes("UTF-8"));
            outputStream.write("\r\n".getBytes("UTF-8"));
            outputStream.write(responseBytes);

            outputStream.flush();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }


}
